package o;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Message;
import android.text.TextUtils;
import com.huawei.android.app.ActivityManagerEx;
import com.huawei.appgallery.packagemanager.api.bean.ManagerTask;
import com.huawei.appgallery.packagemanager.impl.install.control.PackageInstallObserver;
import com.huawei.appgallery.packagemanager.impl.install.process.PackageInstallerActivity;
import com.huawei.appgallery.packagemanager.impl.utils.HMFUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import o.cof.a;

public abstract class cps {
    public static void ˊ(Context context, ManagerTask managerTask) {
        if ((managerTask.flag & 1) == 1) {
            if (!ˎ(context, managerTask)) {
                if (context.getPackageName().equals(managerTask.packageName)) {
                    ll.ˏ(context).ˎ(new Intent("PackageManager.action.CLIENT_SELF_INSTALL_EXECPTION"));
                }
                managerTask.status = cnz.NOT_HANDLER;
                if (ˎ(managerTask) || (managerTask.flag & 2) != 2) {
                    new cpl(context, managerTask).ˏ(managerTask.packageName, -10001);
                    return;
                }
                managerTask.ˊ = true;
                ʻ(context, managerTask);
            }
        } else if ((managerTask.flag & 2) == 2) {
            ʻ(context, managerTask);
        } else {
            managerTask.status = cnz.NOT_HANDLER;
            cnu.ˏ.ˊ("InstallProcess", "can not find any install type for your task," + managerTask.taskId);
            if (managerTask.mode == 2) {
                if (cop.ॱ().ˏ(managerTask, 3)) {
                    cpq.ˋ(managerTask);
                }
            } else if (managerTask.processType == cod.INSTALL && managerTask.mode == 1) {
                cpa.ˊ().ˋ(context, managerTask);
            }
            cph.ॱ(context).ˋ(managerTask.packageName, 1, cnz.NOT_HANDLER, -10008, 5, managerTask.taskId, cod.INSTALL, cod.INSTALL_EXISTING_PKG);
        }
    }

    private static boolean ˎ(Context context, ManagerTask managerTask) {
        StringBuilder append = new StringBuilder().append("InstallProcess").append(" innerInstall begin:").append(managerTask.packageName).append(",");
        for (a aVar : managerTask.apkInfos) {
            append.append(aVar.toString());
        }
        cnu.ˏ.ˋ("InstallProcess", append.toString());
        managerTask.ᐝ = 1;
        if (managerTask.processType == cod.INSTALL) {
            if (ॱ(context, managerTask)) {
                return true;
            }
        } else if (ˋ(context, managerTask)) {
            return true;
        }
        cnu.ˏ.ˊ("InstallProcess", "inner install failed!!!!");
        return false;
    }

    private static boolean ˋ(Context context, ManagerTask managerTask) {
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageManager.class.getMethod("installExistingPackage", new Class[]{String.class}).invoke(packageManager, new Object[]{managerTask.packageName});
            new cpl(context, managerTask).ॱ(managerTask.packageName, 1);
            cnu.ˏ.ˋ("InstallProcess", "installExistingPackage end!" + managerTask.packageName);
            return true;
        } catch (Throwable th) {
            if (th instanceof NameNotFoundException) {
                new cpl(context, managerTask).ॱ(managerTask.packageName, -2);
                return true;
            }
            cnu.ˏ.ˊ("InstallProcess", "installExistingPackage exception: ", th);
            return false;
        }
    }

    private static boolean ॱ(Context context, ManagerTask managerTask) {
        PackageManager packageManager = context.getPackageManager();
        try {
            if ((((cnw) HMFUtils.ˊ(cnw.class)).ˋ(context) & 2) == 0) {
                cnu.ˏ.ˊ("InstallProcess", " can not inner install!pkg:" + managerTask.packageName);
                return false;
            } else if (!ˎ(context, managerTask, 2, packageManager)) {
                return false;
            } else {
                cnu.ˏ.ˋ("InstallProcess", "inner install end!" + managerTask.packageName);
                return true;
            }
        } catch (Throwable e) {
            cnu.ˏ.ˊ("InstallProcess", "installPackage IllegalArgumentException ", e);
            return false;
        } catch (Throwable e2) {
            cnu.ˏ.ˊ("InstallProcess", "inner install exception: ", e2);
            return false;
        }
    }

    private static boolean ˎ(Context context, ManagerTask managerTask, int i, PackageManager packageManager) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (ॱॱ(context, managerTask)) {
            return true;
        }
        if (context.getPackageName().equals(managerTask.packageName)) {
            ll.ˏ(context).ˎ(new Intent("PackageManager.action.CLIENT_SELF_INSTALL"));
        }
        if (ˎ(managerTask) || VERSION.SDK_INT >= 24) {
            int ॱ = cpt.ॱ(context, managerTask, true);
            if (-2 == ॱ) {
                ॱ = cpt.ॱ(context, managerTask, false);
            }
            if (-1 == ॱ) {
                cnu.ˏ.ˊ("InstallProcess", "can not inner install!pkg:" + managerTask.packageName);
                return false;
            } else if (2 == ॱ) {
                cnu.ˏ.ˋ("InstallProcess", "sdk24 install failed:INSTALL_FAILED_INSUFFICIENT_STORAGE");
                new cpl(context, managerTask).ˏ(managerTask.packageName, -4);
            } else if (ॱ == 0) {
                cnu.ˏ.ˋ("InstallProcess", "sdk24 install failed,IOExecption.");
                new cpl(context, managerTask).ˏ(managerTask.packageName, -10006);
            }
        } else {
            Uri fromFile = Uri.fromFile(new File(((a) managerTask.apkInfos.get(0)).ˎ));
            PackageManager.class.getMethod("installPackage", new Class[]{Uri.class, IPackageInstallObserver.class, Integer.TYPE, String.class}).invoke(packageManager, new Object[]{fromFile, new PackageInstallObserver(context, managerTask), Integer.valueOf(i), context.getPackageName()});
        }
        return true;
    }

    private static boolean ॱॱ(Context context, ManagerTask managerTask) {
        if ((managerTask.flag & 8) == 8 && ˊ(context, managerTask.packageName)) {
            Logger.ˎ("InstallProcess", "app is using:" + managerTask.packageName);
            new cpl(context, managerTask).ˏ(managerTask.packageName, -10009);
            return true;
        } else if ((managerTask.flag & 32) == 32 && ˋ()) {
            Logger.ˎ("InstallProcess", "game is runing: " + managerTask.packageName);
            new cpl(context, managerTask).ˏ(managerTask.packageName, -10011);
            return true;
        } else if ((managerTask.flag & DnsConfig.MAX_CACHE_ENTRIES) == DnsConfig.MAX_CACHE_ENTRIES && ˏ(context, managerTask.packageName)) {
            Logger.ˎ("InstallProcess", "navigation app is using: " + managerTask.packageName);
            new cpl(context, managerTask).ˏ(managerTask.packageName, -10013);
            return true;
        } else if ((managerTask.flag & 64) != 64 || !ˋ(context, managerTask.packageName)) {
            return false;
        } else {
            Logger.ˎ("InstallProcess", "music is playing: " + managerTask.packageName);
            new cpl(context, managerTask).ˏ(managerTask.packageName, -10012);
            return true;
        }
    }

    private static boolean ˎ(ManagerTask managerTask) {
        if (managerTask.apkInfos.size() <= 1 && "base".equals(((a) managerTask.apkInfos.get(0)).ˏ) && ((a) managerTask.apkInfos.get(0)).ॱ == 0) {
            return false;
        }
        return true;
    }

    private static void ʻ(Context context, ManagerTask managerTask) {
        String str = ((a) managerTask.apkInfos.get(0)).ˎ;
        cnu.ˏ.ˋ("InstallProcess", " system install:" + managerTask.packageName + "," + str);
        managerTask.ᐝ = 2;
        Intent intent = new Intent(context, PackageInstallerActivity.class);
        intent.putExtra(com.huawei.fastengine.fastview.download.install.PackageInstallerActivity.INSTALL_PATH, str);
        intent.putExtra(com.huawei.fastengine.fastview.download.install.PackageInstallerActivity.INSTALL_PACKAGENAME, managerTask.packageName);
        intent.putExtra("install_taskId", managerTask.taskId);
        intent.putExtra(com.huawei.fastengine.fastview.download.install.PackageInstallerActivity.INSTALL_EXISTING, managerTask.processType == cod.INSTALL_EXISTING_PKG);
        intent.putExtra("install_change_backup_path", managerTask.ˏ);
        intent.setFlags(402653184);
        try {
            int hashCode;
            Message obtainMessage = cov.ˏ(context).obtainMessage();
            if (managerTask.processType == cod.INSTALL_EXISTING_PKG) {
                hashCode = ("hwInstallExisting:" + managerTask.packageName).hashCode();
            } else {
                hashCode = str.hashCode();
            }
            obtainMessage.what = hashCode;
            obtainMessage.obj = managerTask;
            cov.ˏ(context).sendMessageDelayed(obtainMessage, 5000);
            context.startActivity(intent);
        } catch (Throwable e) {
            cnu.ˏ.ˊ("InstallProcess", "can not start install !", e);
            ˏ(context, managerTask);
        }
    }

    public static void ˏ(Context context, ManagerTask managerTask) {
        cnu.ˏ.ˋ("InstallProcess", "startPackageInstallerActivityFailed pkg :" + managerTask.packageName + ",returnCode:" + -10002);
        if (managerTask.mode == 2) {
            if (cop.ॱ().ˏ(managerTask, 3)) {
                cpq.ˋ(managerTask);
            }
        } else if (managerTask.processType == cod.INSTALL && managerTask.mode == 1) {
            cpa.ˊ().ˋ(context, managerTask);
        }
        cph.ॱ(context).ˋ(managerTask.packageName, 4, cnz.NOT_HANDLER, -10002, 5, managerTask.taskId, cod.INSTALL, cod.INSTALL_EXISTING_PKG);
    }

    public static Intent ˎ(Context context, String str) {
        Intent intent = new Intent();
        if (!(context instanceof Activity)) {
            intent.setFlags(268435456);
        }
        intent.setAction("android.intent.action.INSTALL_PACKAGE");
        intent.setData(Uri.parse("package:" + str));
        return intent;
    }

    public static Intent ॱ(Context context, String str) {
        Intent intent = new Intent();
        if (!(context instanceof Activity)) {
            intent.setFlags(268435456);
        }
        intent.setAction("android.intent.action.INSTALL_PACKAGE");
        intent.setData(Uri.fromFile(new File(str)));
        return intent;
    }

    public static boolean ˊ(Context context, String str) {
        List runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
        if (!(runningTasks == null || runningTasks.isEmpty())) {
            Object packageName = ((RunningTaskInfo) runningTasks.get(0)).topActivity.getPackageName();
            if (!TextUtils.isEmpty(packageName) && packageName.equals(str)) {
                return true;
            }
        }
        return false;
    }

    private static boolean ˋ() {
        try {
            return ActivityManagerEx.isInGameSpace(ActivityManagerEx.getLastResumedActivity().packageName);
        } catch (Throwable th) {
            cnu.ˏ.ˋ("InstallProcess", "can not get Game running status: " + th.toString());
            return false;
        }
    }

    private static boolean ˋ(Context context, String str) {
        if (!bmt.ॱ(context)) {
            Logger.ˎ("InstallProcess", "no music playing, packageName: " + str);
            return false;
        } else if (ˏ(context, str)) {
            return true;
        } else {
            Logger.ˎ("InstallProcess", "app is not running foreground service, packageName: " + str);
            return false;
        }
    }

    private static boolean ˏ(Context context, String str) {
        if (context == null || TextUtils.isEmpty(str)) {
            return false;
        }
        ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
        List runningAppProcesses;
        if (activityManager != null) {
            runningAppProcesses = activityManager.getRunningAppProcesses();
        } else {
            runningAppProcesses = null;
        }
        if (r0 == null) {
            cnu.ˏ.ˎ("InstallProcess", "appProcess is null");
            return false;
        }
        for (RunningAppProcessInfo runningAppProcessInfo : r0) {
            Object obj;
            for (Object equals : runningAppProcessInfo.pkgList) {
                if (str.equals(equals)) {
                    obj = 1;
                    break;
                }
            }
            obj = null;
            if (obj != null && (runningAppProcessInfo.importance == 125 || runningAppProcessInfo.importance == 100)) {
                return true;
            }
        }
        return false;
    }
}
